package com.hung.service.serviceImpl;

import com.hung.dao.CommentDao;
import com.hung.dao.UserDao;
import com.hung.pojo.Comment;
import com.hung.service.CommentService;
import com.hung.util.orm.sqlsession.SqlSession;
import com.hung.util.orm.sqlsession.defaults.DefaultSqlSession;

import java.util.List;

/**
 * @author Hung
 */
public class CommentServiceImpl implements CommentService {
    /**
     * 使用工厂创建sqlSession对象
     */
    SqlSession<CommentDao> sqlSession = new DefaultSqlSession();
    /**
     * 使用SqlSession创建Dao接口的代理对象
     */
    CommentDao commentDao = sqlSession.getMapper(CommentDao.class);
    UserDao userDao = new DefaultSqlSession<UserDao>().getMapper(UserDao.class);

    /**
     * 根据mid查询所有评论
     *
     * @param mid
     * @return
     */
    @Override
    public List<Comment> queryAllCommentsByMid(Integer mid) {
        List<Comment> comments = commentDao.queryAllCommentsByMid(mid);
        //根据uid获取nickName
        for (Comment comment : comments) {
            int uid = comment.getUid();
            String s = userDao.queryNickNameById(uid);
            comment.setNickName(s);
        }
        return comments;
    }

    /**
     * 根据id删除评论
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteCommentById(Integer id) {
        return commentDao.deleteCommentById(id) > 0;
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public boolean addComment(Comment comment) {
        return commentDao.addComment(comment) > 0;
    }
}
